package com.bond.service;

import com.bond.common.ScanCount;
import com.bond.common.ScanListReq;
import com.bond.common.ScanReq;
import com.bond.dao.DeviceInfoMapper;
import com.bond.dao.ScanReqInfoMapper;
import com.bond.entry.DeviceInfo;
import com.bond.entry.ScanReqInfo;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

@Slf4j
@Service
public class SysService {

    @Autowired
    private ScanReqInfoMapper scanReqInfoMapper;

    @Autowired
    private DeviceInfoMapper deviceInfoMapper;

    public void distribution(DeviceInfo deviceInfo) {
        deviceInfoMapper.insert(deviceInfo);
    }

    public String insertScanReqInfo(ScanReq req) {
        ScanReqInfo reqInfo = new ScanReqInfo();
        //查询设备分配员工信息
        DeviceInfo deviceInfo = deviceInfoMapper.selectByDeviceId(req.getId());
        if (deviceInfo == null) {
            log.error("设备未完成员工分配信息录入，请先完成'分配扫码枪' deviceId={}", req.getId());
            return "设备未完成员工分配信息录入，请先完成'分配扫码枪' deviceId=" + req.getId();
        }
        reqInfo.setName(deviceInfo.getName());
        reqInfo.setWorkNumber(deviceInfo.getWorkNumber());
        reqInfo.setDeviceId(req.getId());
        reqInfo.setMsg(req.getMsg());
        scanReqInfoMapper.insert(reqInfo);
        return null;
    }

    public List<ScanCount> scanCout() {
        return scanReqInfoMapper.scanCount();
    }

    public List<ScanReqInfo> scanlist(ScanListReq scanListReq) {
        return scanReqInfoMapper.listByDeviceId(scanListReq);
    }

    public List<DeviceInfo> selectDeviceInfoList(DeviceInfo deviceInfo) {
        return deviceInfoMapper.selectDeviceInfoList(deviceInfo);
    }

    public boolean distributionCheckExists(DeviceInfo deviceInfo) {
        PageHelper.startPage(1, 1);
        List<DeviceInfo> list = deviceInfoMapper.selectDeviceInfoList(deviceInfo);
        if (CollectionUtils.isEmpty(list)) {
            return true;
        }
        return false;
    }

//// 设置分页信息：第1页，每页10条记录
//PageHelper.startPage(1, 10);
//
//// 执行查询操作
//List<YourEntity> list = yourMapper.selectYourEntities();
//
//// 将查询结果封装到PageInfo对象中
//PageInfo<YourEntity> pageInfo = new PageInfo<>(list);
//
//// 获取总记录数
//long total = pageInfo.getTotal();

}
